﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElectronicCommerce.Framework.Collections;
using ElectronicCommerce.Framework.Diagnostics;

namespace ElectronicCommerce.Framework.ServiceModel.Configuration
{

    /// <summary>
    /// 表示服务配置元素集合。
    /// </summary>
    public class ServiceElementCollection : KeyedCollection<ServiceElement>
    {
        #region [ Fields ]

        private Dictionary<string, KeyedCollection<ServiceElement>> typedServiceElements
            = new Dictionary<string, KeyedCollection<ServiceElement>>(StringComparer.InvariantCultureIgnoreCase);

        #endregion

        #region [ Properties ]

        /// <summary>
        /// 
        /// </summary>
        public Dictionary<string, KeyedCollection<ServiceElement>> TypedServiceElements
        {
            get
            {
                return typedServiceElements;
            }
        }

        #endregion

        #region [ Methods ]

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <param name="item"></param>
        protected override void InsertItem(int index, ServiceElement item)
        {
            Check.Argument.IsNotNull("item", item);

            KeyedCollection<ServiceElement> services = null;
            if (!typedServiceElements.TryGetValue(item.ServiceTypeName, out services))
            {
                if (services == null)
                {
                    services = new KeyedCollection<ServiceElement>();
                }

                services.AddAndReplace(item);
                typedServiceElements[item.ServiceTypeName] = services;
            }

            base.InsertItem(index, item);
        }

        #endregion
    }
}
